//
//  PlayerViewController.m
//  openEye
//
//  Created by 徐悟源 on 16/4/1.
//  Copyright © 2016年 Wuyuan. All rights reserved.
//

#import "PlayerViewController.h"
#import "VideoView.h"
#import "play.h"
#import "Video.h"

@interface PlayerViewController ()<VideoViewDelegate>

@property (nonatomic,strong) VideoView *player;

@property (nonatomic,strong) UIActivityIndicatorView *activityIndicator;

@end

@implementation PlayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blackColor];
    
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
    play *aPlay = [self.video.playInfo firstObject];
    [self preparePlayerWithURL:[NSURL URLWithString:aPlay.url]];
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft;
}

- (void)preparePlayerWithURL:(NSURL *)url
{
    
    _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    
    [self.activityIndicator startAnimating];
    
    self.activityIndicator.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.activityIndicator];

    [self.activityIndicator mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view.centerX);
        make.centerY.equalTo(self.view.centerY);
        make.width.equalTo(30);
        make.height.equalTo(30);
        
    }];
    [self.activityIndicator startAnimating];
    
    self.activityIndicator.translatesAutoresizingMaskIntoConstraints = NO;
    

    
    [self setPlayer:nil];
    
    // 视频时长
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
    [asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"duration"] completionHandler:^{
        NSError *error = nil;
        
        switch ([asset statusOfValueForKey:@"duration" error:&error]) {
            case AVKeyValueStatusLoaded:
                [self.activityIndicator stopAnimating];
                //[activityView removeFromSuperview];
                [self setupAndStartPlaying:asset.URL];
                break;
                
            default:
                WYLog(@"%s:%@",__func__,[error localizedDescription]);
                
                break;
        }
    }];
}

- (void)setupAndStartPlaying:(NSURL *)url
{
    dispatch_async(dispatch_get_main_queue(), ^{
       
        self.player.delegate = nil;
        [self.player removeFromSuperview];
        self.player = nil;
        
        [self setupPlayer:url];
        
        [self.player play];
    });
}

- (void)setupPlayer:(NSURL *)url
{
    VideoView *videoView = [[VideoView alloc] initWithFrame:CGRectMake(0, 0, self.view.my_Width, self.view.my_Height) contentURL:url];
    videoView.shouldShowHideParentNavigationBar = NO;
    videoView.tintColor = [UIColor whiteColor];
    videoView.titleName = self.video.title;
    [self.view addSubview:videoView];
    videoView.translatesAutoresizingMaskIntoConstraints = NO ;
    
    videoView.delegate = self;
    
    // 是否启动audioSession
    videoView.shouldPlayAudioOnVibrate = NO;
    
    [videoView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.view.left);
        make.right.equalTo(self.view.right);
        make.top.equalTo(self.view.top);
        make.bottom.equalTo(self.view.bottom);
        
    }];
    self.player = videoView;

}

#pragma mark - videoView的代理方法
- (void)pageBackButtonAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
